package com.starblink.android.common.view.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView


class StaggeredItemDecoration(
    private val space: Int,
) :
    RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        //设置上下间距
        if (parent.getChildAdapterPosition(view) == 0 || parent.getChildAdapterPosition(view) == 1) {
            outRect.top = space
        } else {
            outRect.bottom = space
        }

        //判断左右列，方法1
        //设置列间距
        if (parent.getChildAdapterPosition(view) % 2 == 0) {
            //左列
            outRect.right = space
        } else {
            //右列
            outRect.left = space
        }

        //判断左右列，方法2（推荐）
//        GridLayoutManager.LayoutParams params = ((GridLayoutManager.LayoutParams) view.getLayoutParams());
//        if (params.getSpanIndex() != GridLayoutManager.LayoutParams.INVALID_SPAN_ID) {
//            //getSpanIndex方法不管控件高度如何，始终都是左右左右返回index
//            if (params.getSpanIndex() % 2 == 0) {
//                //左列
//                outRect.right = space / 2;
//            } else {
//                //右列
//                outRect.left = space / 2;
//            }
//        }
    }
}